<?php

function unpack_data($data)
{
	$step = 4;
	$xor = ord(substr($data,-1,1));
	$data = substr($data,0,-1);
	for($i=(strlen($data)-1);$i>=0;$i--)
	{
		$xor -= $step;
  	if($xor<0)
			$xor += 256;
		$data[$i] = chr(ord($data[$i]) ^ $xor);
	}
	return json_decode($data,true);
}

function pack_data($data)
{
	$step = 4;
	$data = json_encode($data);
	$xor = rand(0,255);
	for($i=0;$i<strlen($data);$i++)
  {
    $data[$i] = chr(ord($data[$i]) ^ $xor);
		$xor += $step;
    if($xor>255)
      $xor -= 256;
  }
  $data .= chr($xor);
  return $data;
}




$dev = '';
if(isset($GLOBALS["HTTP_RAW_POST_DATA"]) AND $GLOBALS["HTTP_RAW_POST_DATA"])
{
	$dev = $GLOBALS["HTTP_RAW_POST_DATA"];
}


/*

	MAIN CODE

*/

if($dev)
{
	$data = unpack_data($dev);
	
	if($data)
	{
		$action = $data['action'];
		$game = $data['game'];
		
		switch($action)
		{
			case 'init':
				$ret = array();
				$ret['result'] = 'OK';
				$ret['balance'] = 10000;
				$ret['bets'] = array(1,2,3,4,5,6,7,8,9);
				
				
				
				
				$s = pack_data($ret);
				echo $s;
			break;
			case 'spin':
			
			$ret = array();
			$ret['result'] = 'OK';
			$ret['balance'] = 10020;
			$ret['win'] = 100;
			$ret['symbols'] = array();
			for($i=0;$i<15;$i++)
			{
				$ret['symbols'][]=rand(1,12);
			}
			echo pack_data($ret);
			break;
      
      case 'free':
			
			$ret = array();
			$ret['result'] = 'OK';
			$ret['balance'] = 10020;
			$ret['win'] = 100;
			$ret['symbols'] = array();
			for($i=0;$i<15;$i++)
			{
				$ret['symbols'][]=rand(1,12);
			}
			echo pack_data($ret);
			break;
      
			default:
				echo 'ERROR';
			return;
		}
		
		
		
		
	}
}
























